dnl #					-*- Sh -*-
dnl # Copyright (C) 1999-2001, Andrew Arensburger.
dnl # You may distribute this file under the terms of the Artistic
dnl # License, as specified in the README file.
dnl #
dnl # $Id: configure.in,v 1.126.2.1 2001-06-30 07:18:51 arensb Exp $
dnl #

dnl # XXX - Check for strtoul(). If nonexistent, try using atol() in palment.c

dnl # XXX - Test for MAXHOSTNAMELEN, MAXPATHLEN

dnl # XXX - If "tar --version" exits with zero status and contains the
dnl # string "GNU", then it's GNU tar, and we can use
dnl #	GZIP="--best" ${TAR} chozf ${TARBALL} ${DISTDIR}
dnl # Otherwise, fall back on that old stand-by
dnl #	tar cf ${TARBALL} ${DISTDIR}
dnl #	${COMPRESS} ${TARBALL}
dnl # So also check for 'gzip'; if it doesn't exist, fall back on
dnl # 'compress'

dnl # XXX - Might want to check for socket(), <netinet/in.h> etc., and
dnl # turn off NetSync if they don't exist.

dnl # XXX - Check for setvbuf(). Not sure what to do if it doesn't
dnl # exist, though. #define setvbuf() out of existence and hope for
dnl # the best?

dnl # XXX - It should be possible to specify the syslog facility.

AC_INIT(src/coldsync.c)

dnl # Automake does this, but only after all of the variables have
dnl # been defined, which is too late to put the path variables in an
dnl # autogenerated .h file.
test "x$prefix" = xNONE && prefix=${ac_default_prefix}

EXIT_STATUS=0

PACKAGE="coldsync"
VERSION="2.1.3"

dnl # Autoconf 2.13 or higher required for the three-argument version
dnl # of AC_DEFINE_UNQUOTED
AC_PREREQ(2.13)
AC_DEFINE_UNQUOTED(PACKAGE,["$PACKAGE"],[Name of package])
AC_DEFINE_UNQUOTED(VERSION,["$VERSION"],[Version number of package])
if test x"$EDITION" = x ; then
	EDITION="$VERSION"
fi
AC_SUBST(PACKAGE)
AC_SUBST(VERSION)
AC_SUBST(EDITION)

# This is almost certainly a Good Thing
# XXX - Shouldn't CPPFLAGS make its way into "Make.rules"?
LDFLAGS="-L${prefix}/lib"
CPPFLAGS="-I${prefix}/include"

## Checks for programs.

AC_PROG_CC

# Pedantic compilation. Enabled by default if using gcc.
AC_ARG_ENABLE(pedantic,dnl
[  --disable-pedantic      Turn off pedantic error-checking during compilation.],dnl
	cs_cv_pedantic=no,
	AC_CACHE_VAL(cs_cv_pedantic,
		cs_cv_pedantic=yes
	)
)

if test x"$cs_cv_pedantic" = xyes; then
	# I know how to be pedantic with gcc. Other compilers, I'm not sure.
	if test x"$GCC" = x"yes" ; then
		CFLAGS="-Wall -ansi -pedantic $CFLAGS"
		CXXFLAGS="-Wall -ansi -pedantic $CXXFLAGS"
	else
		AC_MSG_RESULT(dnl
[	You don't seem to be using gcc, so various anal retentive
	compiler options won't be turned on. If you know what they
	are for your compiler, you may wish to turn them on, e.g.
		CFLAGS=\"-Wall -ansi -pedantic\" ./configure
		CXXFLAGS=\"-Wall -ansi -pedantic\" ./configure])
	fi
fi
AC_SUBST(CFLAGS)

# AIX sucks so bad, it requires its own special check.
AC_AIX

AC_PROG_CXX

## Test C++ compiler characteristics
dnl # Save the current language
AC_LANG_SAVE
dnl # Make C++ the current language for testing programs
AC_LANG_CPLUSPLUS

# Make sure C++ compiler understands 'bool' (yes, I've gotten mail
# from someone who complained that his compiler didn't understand it).
AC_MSG_CHECKING([whether the C++ compiler understands 'bool'])
AC_TRY_COMPILE([],
	[bool foo; foo = true;],
	AC_MSG_RESULT(yes),
	AC_MSG_WARN([
	Hrung! C++ compiler no understand 'bool' keyword.
	You make ftp magic[,] install new version[,] make spirits happy.

	(Translation: you seem to be using an ancient C++ compiler that
	doesn't understand the 'bool' keyword. You need to upgrade to a
	newer compiler.)])
	EXIT_STATUS=1)
dnl #'

# Make sure C++ compiler understands new-style casts
# ("static_cast<int>(foo)" rather than "(int) foo").

AC_MSG_CHECKING([whether the C++ compiler understands new-style casts])
AC_TRY_COMPILE([],
	[int i; char c = 3; i = static_cast<int>(c);],
	AC_MSG_RESULT(yes),
	AC_MSG_WARN([
	Alack! This venerable compiler doth not understand the sweet
	'static_cast'[,] of which the poet Meyers doth sing. An you heed
	my advice[,] gentle[,] hie to the land of GNU[,] to fetch the compiler
	thereof[,] and much merriment shall ensue.

	(Translation: you need to install a C++ compiler that understands
	new-style casts such as \"static_cast<int>(foo)\")])
	EXIT_STATUS=1)

dnl # Restore the old language
AC_LANG_RESTORE

AC_PROG_RANLIB
AC_PROG_INSTALL

dnl # This is basically the same as AC_PROG_LEX, but it uses
dnl # AC_CHECK_PROGS to make sure that there's a working 'lex'.
dnl # XXX - If $LEX isn't set, ought to check $FLEX
AC_CHECK_PROGS(LEX, flex)
if test -z "$LEX"; then
	AC_MSG_WARN([
	flex could be found. Please install it, then run configure again.])
	LEX=true		# Just in case the bozo tries to build anyway
	EXIT_STATUS=1
fi
if test -z "$LEXLIB"
then
	AC_CHECK_LIB(fl, yywrap, LEXLIB="-lfl")
fi
AC_SUBST(LEXLIB)
AC_SUBST(LEXARGS)		# Not defined by 'configure'. Just a hook.

# I18N (optional, enabled by default)
AC_ARG_WITH(i18n,dnl
[  --without-i18n          Turn off internationalization support.],dnl
:,dnl
with_i18n=yes
)

# The above just checked whether the user asked for --with-i18n, and
# set a default. Now we look for -lintl and the various catalog-making
# utilities.

# Solaris's 'xgettext' is unsuitable, since it only looks for
#	gettext("some string")
# and can't be told to look for
#	_("some string")
# Hence, look for the GNU version of 'xgettext'.

if test x"$with_i18n" = x"no"; then
	# Do-nothing defaults, in case the user doesn't want i18n.
	XGETTEXT=true
	GETTEXT=true
	MSGMERGE=true
	MSGFMT=true
	INSTALL_I18N=no
else
	INSTALL_I18N=yes

	# Under Redhat Linux, gettext() is in libc. On other systems,
	# it's in libintl. The following looks for it in both places,
	# and defines HAVE_GETTEXT if gettext() is defined anywhere.
	AC_CHECK_LIB(intl, gettext)
	if test "$ac_cv_lib_intl_gettext" = yes; then
		AC_DEFINE_UNQUOTED(HAVE_GETTEXT)
	else
		AC_CHECK_FUNCS(gettext)
	fi

	# I18n-related utilities
	# Since the GNU utilities have quite useful extensions, we
	# check to see whether these programs are GNU or not.
	CS_CHECK_GNU_PROGS(XGETTEXT, xgettext, true)
	if test "$ac_cv_have_gnu_xgettext" = no; then
		AC_MSG_WARN([
	The $XGETTEXT program is not GNU xgettext. This isn't critical,
	but it means that if you add strings to the source, they will not
	be added to the message catalog.
	(If this message didn't make sense, ignore it)])
		XGETTEXT=true
	fi
	CS_CHECK_GNU_PROGS(GETTEXT, gettext, true)
	CS_CHECK_GNU_PROGS(MSGMERGE, msgmerge, true)

	dnl # XXX - /usr/imports/bin/X11/msgfmt on nomad (DU) does something
	dnl # unexpected. This is the wrong msgfmt
	CS_CHECK_GNU_PROGS(MSGFMT, msgfmt, true)
	if test "$MSGFMT" = "true"; then
		INSTALL_I18N=no
	fi
	if test "$ac_cv_have_gnu_msgfmt" = yes; then
		EXTRA_MSGFMT_ARGS="--statistics --strict"
	else
		EXTRA_MSGFMT_ARGS=""
	fi
fi

AC_SUBST(INSTALL_I18N)
AC_SUBST(XGETTEXT)
AC_SUBST(GETTEXT)
AC_SUBST(MSGMERGE)
AC_SUBST(MSGFMT)
AC_SUBST(EXTRA_MSGFMT_ARGS)

# See if the user specified a $LOCALEDIR. If not, use the default.
if test -z "${LOCALEDIR}"; then
	LOCALEDIR="`eval echo \"${datadir}/locale\"`"
fi
AC_DEFINE_UNQUOTED(LOCALEDIR, "${LOCALEDIR}")

# I'm not using AC_PROG_YACC here because that doesn't actually make
# sure that there is a working yacc.
AC_CHECK_PROGS(YACC, 'bison -y' byacc yacc)
AC_SUBST(YACCARGS)		# Not defined by 'configure'. Just a hook.
if test -z "$YACC"; then
	AC_MSG_WARN([
	Neither yacc nor bison could be found. Please install them, then
	run configure again.])
	YACC=true		# Just in case the bozo tries to build anyway
	EXIT_STATUS=1
fi

AC_PATH_PROG(PERL, perl)
AC_PATH_PROGS(POD2MAN, pod2man, true)
AC_PATH_PROG(SED, sed)
AC_CHECK_PROGS(DATE, date)
AC_CHECK_PROGS(MKDEP,[mkdep makedepend], true)
AC_CHECK_PROGS(MKID, mkid, true)
AC_CHECK_PROGS(TAR,[gtar tar])
AC_CHECK_PROGS(GZIP,[gzip compress])
AC_CHECK_PROGS(LINT,lint)

if test -z "$LINTARGS"; then
	LINTARGS="-hxFH"
fi
AC_SUBST(LINTARGS)

# There are other programs called 'makeinfo' out there. We want the
# GNU one.
CS_CHECK_GNU_PROGS(MAKEINFO, makeinfo, true)
if test "$ac_cv_have_gnu_makeinfo" = no; then
	ac_cv_prog_MEKEINFO=true
fi
if test -z "$ac_cv_prog_MAKEINFO" -o x"$ac_cv_prog_MAKEINFO" = x"true"; then
	AC_MSG_WARN([
	The makeinfo program could not be found. This isn't critical,
	but it means that you won't be able to create releases with
	\"make dist\".])
fi

AC_CHECK_PROGS(TEXI2DVI, [texi2dvi tex], true)
if test -z "$ac_cv_prog_TEXI2DVI" -o x"$ac_cv_prog_TEXI2DVI" = x"true"; then
	AC_MSG_WARN([
	The texi2dvi program could not be found. This isn't critical,
	but it means that you won't be able to create releases with
	\"make dist\".])
fi

AC_CHECK_PROGS(DVIPS, dvips, true)
if test -z "$ac_cv_prog_DVIPS" -o x"$ac_cv_prog_DVIPS" = x"true"; then
	AC_MSG_WARN([
	The dvips program could not be found. This isn't critical,
	but it means that you won't be able to create releases with
	\"make dist\".])
fi

AC_CHECK_PROGS(TEXI2HTML, texi2html, true)
if test -z "$ac_cv_prog_TEXI2HTML" -o x"$ac_cv_prog_TEXI2HTML" = x"true"; then
	AC_MSG_WARN([
	The texi2html program could not be found. This isn't critical,
	but it means that you won't be able to create releases with
	\"make dist\".])
fi

## Checks for libraries.

AC_CHECK_LIB(y, yyerror, LIBYACC="-ly")
AC_SUBST(LIBYACC)

## Checks for header files.

AC_HEADER_STDC
AC_HEADER_DIRENT
CS_DIRENT_TYPE
AC_CHECK_HEADERS([\
	fcntl.h \
	libintl.h \
	locale.h \
	resolv.h \
	strings.h \
	stropts.h \
	termios.h \
	unistd.h \
	arpa/nameser.h \
	sys/select.h \
	sys/sockio.h \
	sys/time.h \
	sys/types.h \
	sys/param.h \
	netinet/in.h
])

#echo "ac_cv_header_blah_h == <$ac_cv_header_blah_h>"	# <>
#echo "ac_cv_header_fcntl_h == <$ac_cv_header_fcntl_h>"	# <yes>

## Checks for typedefs, structures, and compiler characteristics.
AC_C_INLINE
AC_C_CONST
if test x"$cs_cv_pedantic" = xno; then
	# ANSI C does not support "long long"
	AC_CHECK_SIZEOF(long long, 8)
fi
AC_TYPE_MODE_T
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_TYPE_SIGNAL
AC_TYPE_SIZE_T
AC_TYPE_UID_T
AC_HEADER_TIME
dnl AC_STRUCT_TM
AC_STRUCT_TIMEZONE

# socklen_t is defined (if it's defined) in <sys/socket.h>, so can't
# use AC_CHECK_TYPE.
CS_CHECK_TYPE(socklen_t,int,[#include <sys/socket.h>])

# Check for existence of struct sockaddr.sa_len (4.4BSD).
AC_CACHE_CHECK([for sa_len in struct sockaddr], ac_cv_struct_sockaddr_sa_len,
[AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/socket.h>],dnl
	[struct sockaddr sa; sa.sa_len;],
	ac_cv_struct_sockaddr_sa_len=yes,
	ac_cv_struct_sockaddr_sa_len=no)])

if test $ac_cv_struct_sockaddr_sa_len = yes; then
	AC_DEFINE(HAVE_SOCKADDR_SA_LEN)
fi

# XXX - Add "--disable-ipv6", for Suns and other machines where it's
# just too painful to get it working right.

# Check for existence of struct sockaddr_in6
AC_CACHE_CHECK([for struct sockaddr_in6], ac_cv_struct_sockaddr6,
[AC_TRY_COMPILE(dnl
[#include <sys/types.h>
#include <netinet/in.h>],dnl
	[int s; s = sizeof(struct sockaddr_in6);],
	ac_cv_struct_sockaddr6=yes,
	ac_cv_struct_sockaddr6=no)])

if test $ac_cv_struct_sockaddr6 = yes; then
	AC_DEFINE(HAVE_SOCKADDR6)
fi

## Checks for library functions.
AC_CHECK_FUNCS(fpurge \
	getopt \
	memcpy \
	memset \
	bzero \
	mkstemp \
	select \
	rename \
	strchr \
	strncpy \
	cfmakeraw \
	socketpair \
	usleep \
	strcasecmp \
	strncasecmp \
	snprintf \
	vfprintf \
	vsnprintf
)

# Look for inet_pton(). If it's not found, we'll use inet_aton() instead
AC_SEARCH_LIBS(inet_pton, resolv, AC_DEFINE_UNQUOTED(HAVE_INET_PTON))
AC_SEARCH_LIBS(inet_aton, resolv, AC_DEFINE_UNQUOTED(HAVE_INET_ATON))

if test x"$ac_cv_search_inet_pton" = xno; then
	if test x"$ac_cv_search_inet_aton" = xno; then
		AC_MSG_WARN([
	Gah! You have neither inet_pton() nor inet_aton()! You need to
	install a better OS. I bet this one won't even run BLAZEMONGER,
	and that runs on Amigas, toasters, and even dead rodents!])
		EXIT_STATUS=1
	fi
fi
#'

# Look for inet_ntop(). If it's not found, we'll use inet_ntoa() instead
AC_SEARCH_LIBS(inet_ntop, resolv, AC_DEFINE_UNQUOTED(HAVE_INET_NTOP))
AC_SEARCH_LIBS(inet_ntoa, nsl, AC_DEFINE_UNQUOTED(HAVE_INET_NTOA))

if test x"$ac_cv_search_inet_ntop" = xno; then
	if test x"$ac_cv_search_inet_ntoa" = xno; then
		AC_MSG_WARN([
	Gah! You have neither inet_ntop() nor inet_ntoa()! You need to
	install a better OS. I bet this one won't even run BLAZEMONGER,
	and that runs on Amigas, toasters, and even dead rodents!])
		EXIT_STATUS=1
	fi
fi
#'

# Look for gethostbyname2(). If it doesn't exist, we'll use
# gethostbyname() instead.
#
# RedHat 6.0 has gethostbyname() in libnsl, but also has it in
# libc.
# Under UnixWare 7.1.0, gethostbyname() is in libsocket.
AC_SEARCH_LIBS(gethostbyname2, resolv nsl socket,dnl
	AC_DEFINE_UNQUOTED(HAVE_GETHOSTBYNAME2))
AC_SEARCH_LIBS(gethostbyname, resolv nsl socket,dnl
	AC_DEFINE_UNQUOTED(HAVE_GETHOSTBYNAME))

if test x"$ac_cv_search_gethostbyname2" = xno; then
	if test x"$ac_cv_search_gethostbyname" = xno; then
		AC_MSG_WARN([
	Furrfu! You have neither gethostbyname2() nor gethostbyname().
	Here's a nickel, kid. Get yourself a better OS.])
		EXIT_STATUS=1
	fi
fi
#'

# Check for ENODEV error code, for Linux.
AC_CACHE_CHECK([whether ENODEV is defined], ac_cv_enodev,
[AC_TRY_COMPILE(dnl
[#include <errno.h>],dnl
	[int e; e = ENODEV;],
	ac_cv_enodev=yes,
	ac_cv_enodev=no)])
if test $ac_cv_enodev = yes; then
	AC_DEFINE(HAVE_ENODEV)
fi

# Check whether O_BINARY is defined. It's known to exist under Windows
# (NT); not sure whether it's universal or if it's specific to Cygwin,
# but I don't really care.
AC_MSG_CHECKING([whether O_BINARY is defined])
AC_TRY_COMPILE(
	[#include <fcntl.h>],
	[int fd; open("dummy", O_RDONLY | O_BINARY);],
	AC_MSG_RESULT(yes)
	AC_DEFINE_UNQUOTED(HAVE_O_BINARY),

	AC_MSG_RESULT(no))

# Optional USB support
# (Only works under FreeBSD 4.x, for now)

# The --with(out)-usb option only indicates the user's desire. Even if
# it's turned on, 'configure' still checks to see whether it's
# possible to use the USB code. IOW, "--without-i18n" allows the user
# to turn off USB under *BSD, but that's it.
AC_ARG_WITH(usb,dnl
[  --without-usb           Disable USB support for Visor (FreeBSD 4.x only).],
	[WITH_USB="$withval"],[WITH_USB="yes"])

if test x"$WITH_USB" != xno; then
	AC_MSG_CHECKING([for BSD USB support])

	# This code snippet checks several things:
	#   1) <dev/usb/usb.h> exists
	#   2) It contains 'struct usb_device_info'
	#   3) That struct contains a 'vendorNo' field.
	# All three conditions must be true for USB support to be enabled
	AC_TRY_COMPILE(dnl
		[#include <dev/usb/usb.h>],dnl
		[struct usb_device_info udi;
		 udi.vendorNo = 0x082d;],dnl
		[AC_DEFINE_UNQUOTED(WITH_USB)
		AC_MSG_RESULT(yes)],dnl
		[AC_MSG_RESULT(no)])
fi

AC_MSG_CHECKING([for socks behind the sofa])
AC_MSG_RESULT([no])

## Developer-only options
AC_DIVERT_PUSH(AC_DIVERSION_NOTICE)dnl
ac_help="$ac_help
[Options for developers:]"
AC_DIVERT_POP()dnl

# ElectricFence (optional)
AC_ARG_WITH(efence,dnl
[  --with-efence           Use Electric Fence memory checking library],dnl
[AC_CHECK_LIB(efence,malloc)
if test "$ac_cv_lib_efence_malloc" = yes; then
	AC_DEFINE_UNQUOTED(WITH_EFENCE)
else
	AC_MSG_WARN([Can't find ElectricFence library!])
dnl	#' # For Emacs
fi])

# Primitive memory leak detection
AC_ARG_WITH(leak-detection,dnl
[  --with-leak-detection   Enable (very primitive) memory leak detection],dnl
	AC_DEFINE_UNQUOTED(WITH_LEAK_DETECTION)
)

# Print error messages for various required bits that couldn't be found.
if test -z "$ac_cv_func_getopt" -o x"$ac_cv_func_getopt" = x"no" ; then
	AC_MSG_WARN([
	The getopt() function could not be found. This package
	may not compile properly])
	EXIT_STATUS=1
fi

if test -z "$ac_cv_func_select" -o x"$ac_cv_func_select" = x"no" ; then
	AC_MSG_WARN([
	The select() function could not be found. This package
	may not compile properly])
	EXIT_STATUS=1
fi

if test -z "$ac_cv_func_rename" -o x"$ac_cv_func_rename" = x"no" ; then
	AC_MSG_WARN([
	The rename() function could not be found. This package
	may not compile properly])
	EXIT_STATUS=1
fi

if test -z "$ac_cv_func_strncpy" -o x"$ac_cv_func_strncpy" = x"no" ; then
	AC_MSG_WARN([
	The strncpy() function could not be found. This package
	may not compile properly])
	EXIT_STATUS=1
fi

# XXX - If this machine doesn't have socketpair(), would it be worth
# checking to see if it has bidirectional pipe()s? See Stevens for a
# test program.

HAVE_SOCKETPAIR="$ac_cv_func_socketpair"
if test x"$HAVE_SOCKETPAIR" != x"yes"; then
	# If socketpair() wasn't found (in libc), look for it in
	# libsocket; that's where Solaris puts it.
	AC_CHECK_LIB(socket, socketpair,
		LIBS="$LIBS -lsocket"
		HAVE_SOCKETPAIR=yes)
fi

if test x"$HAVE_SOCKETPAIR" != x"yes"; then
	# socketpair() wasn't found anywhere
	AC_MSG_WARN([
	The socketpair() function could not be found. This package
	may not compile properly])
	EXIT_STATUS=1
fi

AC_DEFINE_UNQUOTED(SYSCONFDIR,"`eval echo \"${sysconfdir}\"`")

AC_CONFIG_HEADER(config.h)
AC_OUTPUT([Make.rules
	doc/version.texi])
if test x"$EXIT_STATUS" != x0; then
	AC_MSG_WARN([
	There were problems during configuration. This package may not
	build properly. Please correct the errors reported above and
	try again.])
fi
dnl #'
# Create the Makefile for the Perl module(s)
# IMPORTANT: YOU MUST SPECIFY THE PREFIX!
(cd perl/ColdSync && ${PERL} Makefile.PL PREFIX=${prefix})
AC_OUTPUT_COMMANDS([cd perl/ColdSync && ${PERL} Makefile.PL PREFIX="${prefix}"])

# XXX - I'm not sure this is working quite right everywhere just yet
#  AC_MSG_RESULT(dnl
#  [
#  	Don't forget to run \"make depend\"
#  ])
dnl #'	# For Emacs

exit "$EXIT_STATUS"
